#!/bin/bash
export PATH=$PATH:/Users/yangxiongnan/work/soft/apache-maven-3.6.3/bin

# 作者: 杨雄南
# 用途: 用于修改pom.xml(包括子模块)里的project.version,project.parent.version -SNAPSHOT 部分
# 参数: -p test|prod 对应pom里的profileid 传入profile是降低profile不同环境变量设置的影响
#      -d 需要将-SNAPSHOT 替换后的字符串
#
# 示例1: 将父子的pom.xml project.version、以及其子项目的project.parent.version
#      后缀2023.03.11.01-SNAPSHOT修改为2023.03.11.01.TEST-SNAPSHOT
#      bash deploy/pom_version_prepare.sh -p test -d ".TEST-SNAPSHOT"
# 示例2: 对于service-base、service-dao仅需要修改根pom.xml project.version "-SNAPSHOT" 为 ".RELEASE"
#      bash deploy/pom_version_prepare.sh -p prod -d ".RELEASE"

while getopts m:d:p: flag
do
    case "${flag}" in
        d) dependencyVersionSuffix=${OPTARG};;
        p) profile=${OPTARG};;
        *) echo "params required" && exit 1;;
    esac
done


case $profile in
  'test') ;;
  'prod') ;;
  *) echo "profile:$profile error,profile must be test or prod" && exit 1;;
esac

pom_path="$PWD"/pom.xml

# 读取以及生成新的project.version
OLD_PROJECT_VERSION=`mvn help:evaluate -Dexpression=project.version -q -DforceStdout -f "$pom_path"`
PROJECT_VERSION=`echo $OLD_PROJECT_VERSION | sed  "s/.RELEASE//g;s/-SNAPSHOT//g;s/.TEST//g;"`
PROJECT_VERSION=${PROJECT_VERSION}$dependencyVersionSuffix



# 修改 project.version
printf "\n-> update project.version: %s ->  %s in pom.xml \n" "$OLD_PROJECT_VERSION" "$PROJECT_VERSION"
mvn -q versions:set -fae -P "$profile" -DnewVersion="$PROJECT_VERSION" -f "$pom_path"

echo "-> check project.version ..."
NEW_PROJECT_VERSION=`mvn -P "$profile" help:evaluate -Dexpression=project.version -q -DforceStdout -f $pom_path`

if [ "$NEW_PROJECT_VERSION" != "$PROJECT_VERSION" ]; then
  echo "project.version change fail" && exit 1
fi




